<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\common\model\card\Dealer;
use app\common\model\Member;
use think\exception\DbException;
use think\response\Json;

/**
 * 站内信管理
 *
 * @icon fa fa-circle-o
 */
class Message extends Backend
{

    /**
     * Message模型对象
     * @var \app\common\model\Message
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Message;
        $this->view->assign("sendeDataList", $this->model->getSendeDataList());
        $this->view->assign("typeList", $this->model->getTypeList());
        $this->view->assign("isReadList", $this->model->getIsReadList());
    }


    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $list = $this->model
            ->where($where)
            ->order($sort, $order)
            ->paginate($limit);
        foreach ($list as $row) {
            $row->nicknames=$this->model->getEnzdAttr($row,$row);
        }
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }


    public function see($id){
        $row=$this->model->get($id);
        if(!empty($row)){
            //发送对象:0=用户,1=店铺,2=区域服务商,3=卡经销商,4=分销商
            if(empty($row["sende_data"])){
                $member =\app\common\model\Member::whereIn("id", $row["member_ids"]) ->column('nickname');
                if(!empty($member)){
                    $model=new Member();
                    foreach ($member as &$v){
                        $v=$model->emoji_decode($v);
                    }
                }
            }
            if($row["sende_data"]==1){
                $member =\app\common\model\Merchant::whereIn("id", $row["member_ids"]) ->column('name');
            }
            if($row["sende_data"]==2){
                $member =\app\common\model\Regionservice::whereIn("id", $row["member_ids"]) ->column('name');
            }
            if($row["sende_data"]==3){
                $member =Dealer::whereIn("id", $row["member_ids"]) ->column('name');
            }
            $row["member"]=implode(",", $member);
        }
        $this->view->assign("row",$row);
        return $this->fetch();
    }
}
